package com.newlink.building.widget

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.widget.AppCompatCheckBox
import com.newlink.building.R

/**
 * Created by xiaodong on 2022/9/22.
 */
class Module_Phone_CustomAlertDialog(context: Context) : Dialog(context, R.style.toast_setting_dialog) {

    private lateinit var mBtnCanle: Button
    private lateinit var mBtnConfirm: Button
    private lateinit var mEtCenter: TextView
    private lateinit var mTvTitle: TextView
    private lateinit var mCheckbox: AppCompatCheckBox

    private var mTitle: String = ""
    private var mInputText: String = ""
    private var mNegative: String = ""
    private var mPositive: String = ""
    private var mShowCheckBox: Boolean = false
    private var mCheckBoxTitle: String = ""
    private var negativeListener: OnClickListener? = null
    private var positiveListener: OnClickListener? = null
    private var onCancel: () -> Unit = {}
    private var onConfirm: () -> Unit = {}

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.dialog_custom_alert)

        mBtnCanle = findViewById(R.id.btn_cancel)
        mBtnCanle.text = mNegative

        mBtnConfirm = findViewById(R.id.btn_confirm)
        mBtnConfirm.text = mPositive

        mEtCenter = findViewById(R.id.alert_content)
        mEtCenter.text = mInputText

        mCheckbox = findViewById(R.id.alert_checkbox)
        mCheckbox.visibility = if (mShowCheckBox) View.VISIBLE else View.GONE
        mCheckbox.text = mCheckBoxTitle

        mTvTitle = findViewById(R.id.custom_dialog_title)
//        mTvTitle.setTextColor(Color.GREEN)
//        mTvTitle.setBackgroundColor(Color.RED)
        mTvTitle.text = mTitle

        mBtnCanle.setOnClickListener {
            onCancel()
        }
        mBtnConfirm.setOnClickListener {
            onConfirm()
        }
    }

    fun setContentText(text: String?): Module_Phone_CustomAlertDialog {
        this.mInputText = text.orEmpty()
        return this
    }

    fun setTitle(text: String?): Module_Phone_CustomAlertDialog {
        this.mTitle = text.orEmpty()
        return this
    }

    fun setNegativeButton(
        text: String = context.resources.getString(R.string.cancel_dialog_custom),
        listener: () -> Unit
    ): Module_Phone_CustomAlertDialog {
        this.mNegative = text
        this.onCancel = listener
        return this
    }

    fun setPositiveButton(
        text: String = context.resources.getString(R.string.confirm_dialog_custom),
        listener: () -> Unit
    ): Module_Phone_CustomAlertDialog {
        this.mPositive = text
        this.onConfirm = listener
        return this
    }

    fun isChecked(): Boolean {
        var isChecked = false
        mCheckbox?.let {
            isChecked = it.isChecked
        }
        return isChecked
    }

    fun setCheckboxVisible(text: String?): Module_Phone_CustomAlertDialog {
        this.mShowCheckBox = true
        this.mCheckBoxTitle = text.orEmpty()
        return this
    }

    interface OnClickListener {
        fun onClick(inputText: String)
    }

    override fun dismiss() {
        super.dismiss()
    }
}
